<?php
/**
 * Created by ThinkPHP5.0.22.
 * User: 小涛.
 * Date: 2018/12/28.
 * Time: 23:44.
 */

namespace app\admin\controller;


use app\admin\model\Freight;
use think\Request;
use app\admin\model\Express as ExpressModel;


/**
 * Class Express 物流配送
 * @package app\admin\controller
 */
class Express extends Admin
{
    //列表
    public function index(){
        $request = Request::instance();
        if($request -> isPost()){
            if ($this->verify_authority() != "") {
                return $this->verify_authority();
            }
            $data = $request -> param();
            $data["update_time"] = time();
            $freight = Freight::get(1);
            if ($freight) {
                $freight = new Freight();
                if ($freight -> allowField(true) -> save($data,["id" => 1])) {
                    return "设置成功";
                }
            } else {
                $data["id"] = 1;
                $freight = new Freight($data);
                if($freight -> allowField(true) -> save()){
                    return "设置成功";
                }
            }
            return "设置失败";
        }
        $list = ExpressModel::listExpress();
        $this->assign("list",$list);
        $this->assign("freight",Freight::get(1));
        return $this -> fetch();
    }

    //更新状态
    public function status(){
        $request = Request::instance();
        if($request -> isPost()){
            if($this->verify_authority() != ""){
                return $this->verify_authority();
            }
            $data = $request -> param();
            $id = 0;
            $status = 0;
            if(array_key_exists("id",$data)){
                $id = $data["id"];
            }
            if(array_key_exists("status",$data)){
                $status = $data["status"];
            }
            $edit = ExpressModel::get($id);
            $edit -> status = $status;
            $edit -> update_time = time();
            if($edit -> save()){
                return "操作成功";
            }
            return "操作失败";
        }
    }

}